package com.github.sort;

public class MergeShort {
    private MergeShort(){}
    private static Comparable[] aux;
    public static void mergeShort(Comparable[] R){
        aux=new DataType[R.length];
        mergeSort(R,0,R.length-1);
    }
    private static void mergeSort(Comparable[] R, int low,int high){
        if (high<=low)return;
        int mid=(low+high)/2;
       mergeSort(R,low,mid);
       mergeSort(R,mid+1,high);
       merge(R,low,mid,high);
    }
    private static void merge(Comparable[] R,int low,int mid,int high){
        int i=low,j=mid+1;
        for (int k = low; k <=high; k++) {
            aux[k]=R[k];
        }
        for (int k = low; k <=high ; k++) {
            if (i>mid)R[k]=aux[j++];
            else if (j>high)R[k]=aux[i++];
            else if (less(aux[j],aux[i] ))R[k]=aux[j++];
            else R[k]=aux[i++];
        }
    }
    private static boolean less(Comparable v,Comparable w){
        return v.compareTo(w)<0;
    }
}
